import Vue from 'vue'
import VueRouter from 'vue-router'
import Storage from '@/utils/storage'

Vue.use(VueRouter)

/* Layout */
import Layout from '@/views/layout'

/* 解决重复点击相同导航报错 */
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [{
  path: '/',
  name: 'Home',
  redirect: '/',
  meta: {
    title: '仪表盘',
    icon: 'el-icon-odometer'
  },
  component: Layout,
  children: [{
    path: '/',
    meta: {
      class: '仪表盘',
    },
    component: () => import('../views/Home.vue'),
  }]
},
{
  // 登录
  path: '/login',
  name: 'Login',
  component: () => import('../views/Login.vue')
},
{
  // 测试
  path: '/test',
  name: 'Test',
  component: () => import('../views/Test.vue')
},
{
  path: '/tag',
  meta: {
    title: '标签管理',
    icon: 'el-icon-discount'
  },
  component: Layout,
  children: [{
    path: '/tag/list',
    name: 'Tag',
    meta: {
      class: '标签管理',
      title: '标签列表',
      icon: 'el-icon-document'
    },
    component: () => import('../views/Tag.vue'),
  },
  {
    path: '/tag/create',
    name: 'TagCreate',
    meta: {
      class: '标签管理',
      title: '创建标签',
      icon: 'el-icon-circle-plus-outline'
    },
    component: () => import('../views/components/TagCreate.vue'),
  },
  {
    path: '/tag/edit',
    name: 'TagEdit',
    meta: {
      class: '标签管理',
      title: '编辑标签',
    },
    component: () => import('../views/components/TagEdit.vue'),
  }
  ]
},
{
  path: '/article',
  name: 'Article',
  meta: {
    title: '文章管理',
    icon: 'el-icon-reading'
  },
  component: Layout,
  children: [{
    path: '/article/list',
    meta: {
      class: '文章管理',
      title: '文章列表',
      icon: 'el-icon-tickets'
    },
    component: () => import('../views/Article.vue'),
  },
  {
    path: '/article/temp',
    meta: {
      class: '文章管理',
      title: '草稿箱',
      icon: 'el-icon-receiving'
    },
    component: () => import('../views/components/ArticleTemp.vue'),
  },
  {
    path: '/article/create',
    meta: {
      class: '文章管理',
      title: '创建文章',
      icon: 'el-icon-circle-plus-outline'
    },
    component: () => import('../views/components/ArticleCreate.vue'),
  },
  {
    path: '/article/edit',
    meta: {
      class: '文章管理',
      title: '编辑文章',
    },
    component: () => import('../views/components/ArticleEdit.vue'),
  },
  ]
},
{
  path: '/msg',
  name: 'Meg',
  meta: {
    title: '消息管理',
    icon: 'el-icon-message'
  },
  component: Layout,
  children: [
    // {
    //   path: '/msg/index',
    //   meta: {
    //     title: '消息中心',
    //     icon: 'el-icon-chat-line-square'
    //   },
    //   component: () => import('../views/Msg.vue'),
    // },
    {
      path: '/msg/comment',
      meta: {
        class: '消息管理',
        title: '评论列表',
        icon: 'el-icon-chat-line-square'
      },
      component: () => import('../views/components/MsgComment.vue'),
    },
    {
      path: '/msg/message',
      meta: {
        class: '消息管理',
        title: '留言列表',
        icon: 'el-icon-circle-plus-outline'
      },
      component: () => import('../views/components/MsgMessage.vue'),
    },
    {
      path: '/msg/reply',
      meta: {
        class: '消息管理',
        title: '回复列表',
        icon: 'el-icon-circle-plus-outline'
      },
      component: () => import('../views/components/MsgReply.vue'),
    },
    {
      path: '/msg/person',
      meta: {
        class: '消息管理',
        title: '我的消息',
        icon: 'el-icon-bell'
      },
      component: () => import('../views/components/MsgPerson.vue'),
    },
  ]
}, {
  path: '/user',
  name: 'User',
  meta: {
    title: '成员管理',
    icon: 'el-icon-user'
  },
  component: Layout,
  children: [{
    path: '/user/client',
    meta: {
      class: '成员管理',
      title: '用户管理',
      icon: 'el-icon-coin'
    },
    component: () => import('../views/User.vue'),
  },
  {
    path: '/user/admin',
    meta: {
      class: '成员管理',
      title: '管理员权限',
      icon: 'el-icon-coordinate'
    },
    component: () => import('../views/components/UserAdmin.vue'),
  },
  {
    path: '/user/edit',
    meta: {
      class: '成员管理',
      title: '编辑用户'
    },
    component: () => import('../views/components/UserEdit.vue'),
  },
    {
      path: '/user/admin/edit',
      meta: {
        class: '成员管理',
        title: '编辑管理员权限'
      },
      component: () => import('../views/components/UserAdminEdit.vue'),
    }
  ]
},
{
  path: '/system',
  name: 'System',
  meta: {
    title: '系统管理',
    icon: 'el-icon-setting'
  },
  component: Layout,
  children: [{
      path: '/system/ip',
      meta: {
        class: '系统管理',
        title: 'IP日志',
        icon: 'el-icon-location-outline'
      },
    component: () => import('../views/components/SystemIP.vue'),
    },
  //   {
  //     path: '/system/pc',
  //     meta: {
  //       title: 'Pc',
  //       icon: 'el-icon-setting'
  //     },
  //     component: () => import('../views/components/SystemPc.vue'),
  //   },
  //   {
  //     path: '/system/admin',
  //     meta: {
  //       title: 'Admin',
  //       icon: 'el-icon-setting'
  //     },
  //     component: () => import('../views/components/SystemAdmin.vue'),
  //   }
  ]
},
{
  path: '*',
  name: '404',
  component: () => import('../views/common/404.vue')
}
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let isAuthenticated = Storage.getSession('token')
  if (to.name !== 'Login' && !isAuthenticated) next({
    name: 'Login'
  })
  else next()
})

export default router